package mx.edu.ittoluca.fp;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: Victor Santillán
 * Date: 27/06/12
 * Time: 09:19 PM
 */
public class TryThread extends Thread {

    private String firstName;
    private String secondName;
    private long aWhile;

    public TryThread(String firstName, String secondName, long delay) {
        this.firstName = firstName;
        this.secondName = secondName;
        this.aWhile = delay;
//        setDaemon(true);
    }

    public static void main(String[] args) {
        Thread first = new TryThread("Hopalong", "Casidy", 200L);
        Thread second = new TryThread("Marilyn", "Monroe", 300L);
        Thread third = new TryThread("Slim", "Pickens", 500L);

        System.out.println("Press enter when you had enough...\n");
        first.start();
        second.start();
        third.start();

        try {
            System.in.read();
            System.out.println("Enter pressed...\n");
            first.interrupt();
            second.interrupt();
            third.interrupt();
        } catch (IOException e){
            System.out.println("Ending main()");
            return;
        }
    }

    @Override
    public void run() {
        try {
            while (true) {
                System.out.println(firstName);
                sleep(aWhile);
                System.out.println(secondName + "\n");
            }
        } catch (InterruptedException e) {
            System.out.println(firstName + secondName + e);
        }
    }

}
